********************************************************************************
********************************************************************************
* PROGRAM:		specification_chart
* Project:		Stress, Ethnicity, and Prosocial Behavior
* Author:		Moritz Poll (moritz.poll@brown.edu)
* PIs:			Johannes Haushofer, Sara Lowes, Abednego Musau, David Ndetei, 
*				Nathan Nunn, Moritz Poll, Nancy Qian
* Purpose:		Prepares a specification chart as an appendix summary
********************************************************************************
********************************************************************************
noisily dis as input "Start: Specification Chart"
clear all
global specifications `""_main" "_spec2" "_imbalance" "_sessionfe" "_coa17" "_comprehension" "_viewtime" "_decisiontime" "_continuetime" "_parents_same" "_welsch""'
global extra_specifications `""_ingroup" "_fullsample""'

foreach coeff in pill diff_eth eth_Delta {

********************************************************************************
***************************** Retrieve coefficients ****************************
********************************************************************************

	use "$estimates_dir/dg_`coeff'", clear
	merge 1:1 spec_name using "$estimates_dir/tg1_`coeff'", nogen
	merge 1:1 spec_name using "$estimates_dir/tg2_`coeff'", nogen
	merge 1:1 spec_name using "$estimates_dir/social_proximity_`coeff'", nogen
	if "`coeff'" != "pill" merge 1:1 spec_name using "$estimates_dir/cyd_`coeff'", nogen
	if "`coeff'" != "pill" local cyd "cyd"
	else local cyd ""

	gen keep = 0
	local i = 0
	foreach specification in $specifications {
		replace keep = `++i' if spec_name == "`specification'"
	}
	keep if keep
	sort keep
	drop keep
	if "`coeff'" == "pill" drop if spec_name == "_spec2"

	foreach game in dg tg1 tg2 social_proximity `cyd' {
		capture gen `game'_`coeff' = runiform()
		capture gen `game'_`coeff'_se = 0.1*runiform() + 0.05
		* Confidence Intervals
		gen `game'_`coeff'_h90 = `game'_`coeff' + 1.64*`game'_`coeff'_se
		gen `game'_`coeff'_l90 = `game'_`coeff' - 1.64*`game'_`coeff'_se
		gen `game'_`coeff'_h95 = `game'_`coeff' + 1.96*`game'_`coeff'_se
		gen `game'_`coeff'_l95 = `game'_`coeff' - 1.96*`game'_`coeff'_se
		gen `game'_`coeff'_h99 = `game'_`coeff' + 2.58*`game'_`coeff'_se
		gen `game'_`coeff'_l99 = `game'_`coeff' - 2.58*`game'_`coeff'_se
		* Mean bar should be about equally wide visually regardless of axes
		sum `game'_`coeff'_h99
		local hi = r(mean)
		sum `game'_`coeff'_l99
		local lo = r(mean)
		gen `game'_`coeff'_h0  = `game'_`coeff' + 0.01*(`hi'-`lo')
		gen `game'_`coeff'_l0  = `game'_`coeff' - 0.01*(`hi'-`lo')
	}
	order spec_name dg* tg1* tg2* social_proximity*
	capture egen ymin = rowmin(cyd_`coeff'_l99 dg_`coeff'_l99 tg1_`coeff'_l99 tg2_`coeff'_l99 social_proximity_`coeff'_l99)
	capture egen ymax = rowmax(cyd_`coeff'_h99 dg_`coeff'_h99 tg1_`coeff'_h99 tg2_`coeff'_h99 social_proximity_`coeff'_h99)
	capture egen ymin = rowmin(dg_`coeff'_l99 tg1_`coeff'_l99 tg2_`coeff'_l99 social_proximity_`coeff'_l99)
	capture egen ymax = rowmax(dg_`coeff'_h99 tg1_`coeff'_h99 tg2_`coeff'_h99 social_proximity_`coeff'_h99)

********************************************************************************
***************************** Define specifications ****************************
********************************************************************************

	gen main_specification = spec_name == "_main"
	gen spec = .
	foreach game in `cyd' dg tg1 tg2 social_proximity {
		* Sort specifications
		sort `game'_`coeff' spec_name // The second is to have the sort stable since e.g. different clustering yields identical coefficients
		replace spec = _n
		
		* Define spacing of the specification key legend
		sum ymin
		local labelspacer = 0.01					 *1^("`game'" == "social_proximity")*1.0^("`game'" == "cyd" & "`coeff'" == "diff_eth")*1^("`game'" == "cyd" & "`coeff'" == "eth_Delta")
		local labelbuffer = (abs(min(0,r(min)))+0.01)*1^("`game'" == "social_proximity")*1.0^("`game'" == "cyd" & "`coeff'" == "diff_eth")*1^("`game'" == "cyd" & "`coeff'" == "eth_Delta")
		local i = 0
		foreach specification in $specifications $extra_specifications {
			capture gen spec`++i' = .
			replace spec`i' = -(`labelbuffer'+`labelspacer'*`i' + 2*`labelspacer'*(`i' > 2) + 2*`labelspacer'*(`i' > 6))
		}
		capture gen spec_main = (spec_name != "_spec2")
		capture gen spec_ingroup = (spec_name != "_coa07" & spec_name != "_coa17")
		capture gen spec_fullsample = (spec_name == "_coa07" | spec_name == "_coa17" | spec_name == "_sessionfe" | spec_name == "_spec2" | spec_name == "_imbalance" | spec_name == "_main")

********************************************************************************
****************************** Specification Chart *****************************
********************************************************************************

		local barwidth = 0.5
		local on maroon
		local off maroon*0.25
		local msymbol square
		sum ymin
		local ymin = min(0,ceil(50*r(min))/50)*1^("`game'" == "social_proximity")*1^("`game'" == "cyd")
		local ymin2 =  spec13[1]
		sum ymax
		local ymax = r(max)*1^("`game'" == "social_proximity")*1^("`game'" == "cyd")
		local ystep = 0.02*1^("`game'" == "social_proximity")
		
		local off_scatter ""
		local i = 0
		foreach specification in $specifications $extra_specifications {
			local off_scatter "`off_scatter' (scatter spec`++i'  spec, yaxis(2) color(`off') msymbol(`msymbol'))"
		}

		local j = 0 // scatter on counter
		local i = 0 // Label space counter

		local yscale1_off "off"
		if ("`game'" == "cyd" | "`game'" == "dg" | "`game'" == "social_proximity") local yscale1_off ""
		#delimit ;
		local ylabel1_dg
		`" 
			-`labelbuffer' "{it:In-group definition}" 
			`=-(`labelbuffer'+`++i'*`labelspacer')' "Ethnicity" 
			`=-(`labelbuffer'+`++i'*`labelspacer')' "Political coalitions" 
			`=-(`labelbuffer'+`++i'*`labelspacer')' " "

			`=-(`labelbuffer'+`++i'*`labelspacer')' "{it:Additional controls}" 
			`=-(`labelbuffer'+`++i'*`labelspacer')' "Players' characteristics" 
			`=-(`labelbuffer'+`++i'*`labelspacer')' "Individual FEs" 
			`=-(`labelbuffer'+`++i'*`labelspacer')' "Session FEs" 
			`=-(`labelbuffer'+`++i'*`labelspacer')' "Imbalanced demographics" 
			`=-(`labelbuffer'+`++i'*`labelspacer')' " "

			`=-(`labelbuffer'+`++i'*`labelspacer')' "{it:Omitted observations}" 
			`=-(`labelbuffer'+`++i'*`labelspacer')' "None (full sample)" 
			`=-(`labelbuffer'+`++i'*`labelspacer')' "Poor comprehension" 
			`=-(`labelbuffer'+`++i'*`labelspacer')' "Fast profile views" 
			`=-(`labelbuffer'+`++i'*`labelspacer')' "Fast initial decision" 
			`=-(`labelbuffer'+`++i'*`labelspacer')' "Fast final decision" 
			`=-(`labelbuffer'+`++i'*`labelspacer')' "Mixed ethnicity parents" 
			`=-(`labelbuffer'+`++i'*`labelspacer')' "High-influence outliers"
		"'; #delimit cr
		local ylabel1_cyd `ymin'(0.02)`ymax' `ylabel1_dg'
		local ylabel1_dg  `ymin'(0.02)`ymax' `ylabel1_dg'
		if "`coeff'" != "pill" local ylabel1_dg `ymin'(0.02)`ymax'
		local ylabel1_social_proximity `ymin'(`ystep')`ymax'
	
		preserve
		keep if !missing(`game'_`coeff') // The graph design depends on the number of rows in the data set
		twoway 	(rbar `game'_`coeff'_h99 `game'_`coeff'_l99 spec, color(gs13) barwidth(`barwidth')) ///
				(rbar `game'_`coeff'_h95 `game'_`coeff'_l95 spec, color(gs10) barwidth(`barwidth')) ///
				(rbar `game'_`coeff'_h90 `game'_`coeff'_l90 spec, color(gs8)  barwidth(`barwidth')) ///
				(rbar `game'_`coeff'_h99 `game'_`coeff'_l99 spec if main_specification == 1, color(maroon*0.5) barwidth(`barwidth')) ///
				(rbar `game'_`coeff'_h95 `game'_`coeff'_l95 spec if main_specification == 1, color(maroon*0.8) barwidth(`barwidth')) ///
				(rbar `game'_`coeff'_h90 `game'_`coeff'_l90 spec if main_specification == 1, color(maroon*1.0) barwidth(`barwidth')) ///
				(rbar `game'_`coeff'_h0  `game'_`coeff'_l0  spec, color(gs0)  barwidth(`barwidth')) ///
				`off_scatter' ///
				(scatter spec`++j' spec if spec_name != "_coa07" & spec_name != "_coa17", 					yaxis(2) color(`on') msymbol(`msymbol')) ///
				(scatter spec`++j' spec if spec_name == "_coa17", 					yaxis(2) color(`on') msymbol(`msymbol')) ///
				(scatter spec`++j' spec if spec_main == 1, 							yaxis(2) color(`on') msymbol(`msymbol')) ///
				(scatter spec`++j' spec if spec_name == "_spec2", 					yaxis(2) color(`on') msymbol(`msymbol')) ///
				(scatter spec`++j' spec if spec_name == "_sessionfe", 				yaxis(2) color(`on') msymbol(`msymbol')) ///
				(scatter spec`++j' spec if spec_name == "_imbalance", 				yaxis(2) color(`on') msymbol(`msymbol')) ///
				(scatter spec`++j' spec if spec_fullsample == 1, 					yaxis(2) color(`on') msymbol(`msymbol')) ///
				(scatter spec`++j' spec if spec_name == "_comprehension", 			yaxis(2) color(`on') msymbol(`msymbol')) ///
				(scatter spec`++j' spec if spec_name == "_viewtime", 				yaxis(2) color(`on') msymbol(`msymbol')) ///
				(scatter spec`++j' spec if spec_name == "_decisiontime", 			yaxis(2) color(`on') msymbol(`msymbol')) ///
				(scatter spec`++j' spec if spec_name == "_continuetime", 			yaxis(2) color(`on') msymbol(`msymbol')) ///
				(scatter spec`++j' spec if spec_name == "_parents_same", 			yaxis(2) color(`on') msymbol(`msymbol')) ///
				(scatter spec`++j' spec if spec_name == "_welsch", 					yaxis(2) color(`on') msymbol(`msymbol')) ///
/*Appearance*/		, legend(off)  yline(0, lcolor(black) extend)  ///
/*Value labels*/	ylabel(`ylabel1_`game'', axis(1) format(%03.2f) nogrid noticks angle(0) labsize(medsmall))  yscale(`yscale1_off' range(`ymin2' `ymax') axis(1)) ///
/*Specifications*/	ylabel(none `ylabel2_`game'', axis(2) norescale nogrid noticks angle(0) labsize(medsmall)) yscale(range(`ymin' `ymax') lwidth(0) axis(2)) ///
/*Horizontal axis*/	xlabel(1(1)`=_N', nolabels noticks grid gstyle(minor))  xscale(off) ///
/*Titles*/			xtitle("")  ytitle("")  title(${`game'_name}, size(*0.9) justification(right) color(black)) ///
/*Aspect & Scale*/	aspectratio(`=5.2-0.2*_N')  scale(`=1+0.8*("`game'" == "dg" & "`coeff'" != "pill")+1.2*("`game'" == "tg1" | "`game'" == "tg2")+0.8*("`game'" == "social_proximity")+0.2*("`game'" == "cyd")') ///
/*Horizontal size*/	xsize(`=2+1*("`game'" == "cyd")+1.4*("`game'" == "dg" & "`coeff'" == "pill")+0.175*("`game'" == "dg")-0.15*("`game'" == "tg1" | "`game'" == "tg2")+0.1*("`game'" == "social_proximity")') ///
/*Margins*/			graphregion(margin(`=12*(("`game'" == "dg" & "`coeff'" == "pill") | "`game'" == "cyd")' 0 0 0) color(white))
* The goal of the aspectratio, xsize, margin, and scale adjustments is to get every graph to exactly fill the graph window, so they align exactly in LaTeX.
		
		graph export "$figures_dir/specification_chart_`coeff'_`game'.pdf", replace
		restore
	}
}

noisily dis as input "End: Specification Chart"
